package in.muton.edushare.pages.cp.semCP;

import in.muton.edushare.components.whiteMenu.WhiteMenu;
import in.muton.edushare.pages.cp.CPBase;

import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.vo.Universities;

public class SemesterCP extends CPBase {
	
	private static Log log = LogFactory.getLog(SemesterCP.class);
	public static final ArrayList<String> menuList = new ArrayList<String>();
	private static Model<ArrayList<String>> menuModel;
	static{
		menuList.add("Change Semester");
		menuList.add("Running Courses");
		menuList.add("Elective Allotment");
		menuModel = new Model<ArrayList<String>>(menuList);
	}
	private Panel selectedMenuPanel = new EmptyPanel("selectedMenuPanel");
	private WhiteMenu menu;
	
	public SemesterCP(PageParameters params) {
		super(params);
		loadPage();
	}	
	
	public SemesterCP(IModel<Universities> model) {
		super(model,0);
		loadPage();
	}	
		
	private void loadPage(){
		this.menu = new WhiteMenu("menu", menuModel, 0) {
			
			private static final long serialVersionUID = 6239143282999179486L;

			@Override
			protected void onMenuClick(ListItem<String> menuItem) {
				if(menuItem.getModelObject().equals(menuList.get(0))){
					loadPanel(0);
				}else if(menuItem.getModelObject().equals(menuList.get(1))){
					loadPanel(1);
				}else if(menuItem.getModelObject().equals(menuList.get(2))){
					loadPanel(2);
				}
			}
		};
		add(menu);
		add(selectedMenuPanel);
		//load change semester by default
		loadChangeSemester();
		
	}
	
	/** Takes you to appropriate menu page, based on index. Index is counted on base zero. Index zero is the left most menu */
	public void jumpTo(int menuIndex){
		menu.highlightMenu(menuIndex);
		loadPanel(menuIndex);
	}
	
	private void loadPanel(int index){
		switch (index) {
		case 0:
			loadChangeSemester();
			break;

		case 1:
			loadRunningcourses();
			break;

		case 2:
			loadAllotElectives();
			break;

		default:
			loadChangeSemester();
			break;
		}
	}
	
	private void loadChangeSemester(){
		replacePanel(new ChangeSemesterPanel("selectedMenuPanel", univModel, this));
	}
	
	private void loadRunningcourses(){
		replacePanel(new EmptyPanel("selectedMenuPanel"));
	}
	
	private void loadAllotElectives(){
		replacePanel(new AllotElectivePanel("selectedMenuPanel", univModel, this));
	};
	
	private void replacePanel(Panel replacement) {
		selectedMenuPanel.replaceWith(replacement);
		selectedMenuPanel = replacement;
	}
}
